package lab15notepad;

public class SecureNotepad extends SimpleNotepad {

	private static final int MIN_LENGTH=5;
	private String password;
	private static final String DEFAULT_PASSWORD="password";
	
	public SecureNotepad(String title, int pageNumber, String password) {
		super();
		this.setTitle(title);
		this.setPageNumber(pageNumber);
		this.setPassword(password);
		this.setPages(new Page[pageNumber]);
		for (int i = 0; i < pageNumber; i++) setPage(new Page(), i);
	}
	
	public static boolean passwordSecure (String password) {
		boolean digit = false, lowerCase = false, upperCase = false;
		if (password.length()<MIN_LENGTH) return false;
		for (int i = 0; i < password.length(); i++) {
			if (password.charAt(i)>='0'&&password.charAt(i)<='9') digit=true;
			if (password.charAt(i)>='a'&&password.charAt(i)<='z') lowerCase=true;
			if (password.charAt(i)>='A'&&password.charAt(i)<='Z') upperCase=true;
		}
		return (digit&&upperCase&&lowerCase);
	}
	
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the defaultPassword
	 */
	public static String getDefaultPassword() {
		return DEFAULT_PASSWORD;
	}

	public SecureNotepad() {
		this(getDefaultTitle(), getDefaultPageNumber(), DEFAULT_PASSWORD);
	}
	
	
	private boolean checkPassword() {
		System.out.print("Please enter password: ");
		String password = Input.getLine();
		if (this.password.equals(password)) return true;
		System.out.println("Wrong password.");
		return false;
	}
	
	@Override
	public void setTitle(String title, int page) {
		if (checkPassword()) {
			super.setTitle(title, page);
		}
	}

	@Override
	public void addText(String text, int page) {
		if (checkPassword()) {
			super.addText(text, page);
		}
	}

	@Override
	public void replaceText(String text, int page) {
		if (checkPassword()) {
			super.replaceText(text, page);
		}
	}

	@Override
	public void deleteText(int page) {
		if (checkPassword()) {
			super.deleteText(page);
		}
	}

	@Override
	public void showPage(int page) {
		if (checkPassword()) {
			super.showPage(page);
		}
	}

	@Override
	public void showAll() {
		if (checkPassword()) {
			super.showAll();
		}
	}
	
	@Override
	public void printAllPagesWithDigits() {
		if (checkPassword()) {
			super.printAllPagesWithDigits();
		}
	}
	
	@Override
	public void searchWord(String word) {
		if (checkPassword()) {
			super.searchWord(word);
		}
	}

}
